namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Globalization;
    using System.Net;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Configuration;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class TenantMaintenanceControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantMaintenanceController>()
                .DerivesFrom<TenantController>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .Implements<IRegisterRoutes>()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TenantMaintenanceController());
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new TenantMaintenanceController() as IRegisterRoutes).Register(null));
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();
            (new TenantMaintenanceController() as IRegisterRoutes).Register(routes);

            Assert.Equal<int>(1, routes.Count);
        }

        [Fact]
        public void op_Representation()
        {
            Default.Clear();
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml = "<tenant />"
                });

                Default.Register<Maintenance>("<resource />");

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/test.en.html").Verifiable();
                mock.SetupSet(x => x.Response.StatusCode).Callback(actual => Assert.Equal<int>(actual, (int)HttpStatusCode.ServiceUnavailable)).Verifiable();
                mock.Setup(x => x.Response.Cache.SetCacheability(HttpCacheability.NoCache)).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsAssignableFrom<ViewResult>(new TenantMaintenanceController().HtmlRepresentation());

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                Default.Reset();
                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}